function [dx,dy,dz]=normal_n(x,y,fitresult)
d=0.5;
while true
    if abs(fitresult(x+d,y)-fitresult(x-d,y))<0.001
        while true
            if abs(fitresult(x+d,y)+fitresult(x-d,y)-2*fitresult(x,y))<0.001
                WXD=(fitresult(x+d,y)-fitresult(x,y))/d;
                break
            else
                d=d*0.5;
            end
        end
        break
    else
        d=d*0.5;
    end
    if d==0
        if ~isnan(fitresult(x+0.01,y))&&~isnan(fitresult(x-0.0,y1))
            WXD=min((fitresult(x+0.01,y)-fitresult(x,y))/0.01,(fitresult(x,y)-fitresult(x-0.01,y))/0.01);
            break
        elseif ~isnan(fitresult(x-0.01,y))
            WXD=(fitresult(x,y-0.01)-fitresult(x,y))/0.01;
            break
        elseif ~isnan(fitresult(x+0.01,y))
            WXD=(fitresult(x+0.01,y)-fitresult(x,y))/0.01;
            break
        else
            WXD=0;
            break
        end
    end
end
d=0.5;
while true
    if abs(fitresult(x,y+d)-fitresult(x,y-d))<0.001
        while true
            if abs(fitresult(x,y+d)+fitresult(x,y-d)-2*fitresult(x,y))<0.001
                WYD=(fitresult(x,y+d)-fitresult(x,y))/d;
                break
            else
                d=d*0.5;
            end
        end
        break
    else
        d=d*0.5;
    end
    if d==0
        if ~isnan(fitresult(x,y+0.01))&&~isnan(fitresult(x,y-0.01))
            WYD=min((fitresult(x,y+0.01)-fitresult(x,y))/0.01,(fitresult(x,y)-fitresult(x,y-0.01))/0.01);
            break
        elseif ~isnan(fitresult(x,y-0.01))
            WYD=(fitresult(x,y)-fitresult(x,y-0.01))/0.01;
            break
        elseif ~isnan(fitresult(x,y+0.01))
            WYD=(fitresult(x,y+0.01)-fitresult(x,y))/0.01;
            break
        else
            WYD=0;
        end
    end
end
l=(WXD^2+WYD^2+1)^0.5;
dx=WXD/l; dy=WYD/l; dz=-1/l;
if dz<0
    dx=-dx; dy=-dy; dz=-dz;
end
end